import "package:hinge/common/index.dart";
import "package:mobx/mobx.dart";

part "recommend_store.g.dart";

class RecommendStore = _RecommendStore with _$RecommendStore;

abstract class _RecommendStore with Store {
  @observable
  bool isLoading = false;

  @observable
  ObservableList<PerfumeModel> recommendPerfumes =
      ObservableList<PerfumeModel>.of([]);

  // 点赞状态缓存 - key: perfumeId, value: 是否已点赞
  @observable
  ObservableMap<int, bool> likeStatus = ObservableMap<int, bool>.of({});

  // 收藏状态缓存 - key: perfumeId, value: 是否已收藏
  @observable
  ObservableMap<int, bool> collectStatus = ObservableMap<int, bool>.of({});

  @action
  Future<void> loadRecommendData() async {
    isLoading = true;
    await Future.delayed(const Duration(milliseconds: 300));
    // 加载所有推荐香水
    final all = PerfumeService.getAllPerfumes();
    recommendPerfumes.clear();
    recommendPerfumes.addAll(all.where((p) => p.isRecommend));

    // 更新点赞和收藏状态
    _updateLikeCollectStatus();

    isLoading = false;
  }

  @action
  void _updateLikeCollectStatus() {
    final currentUser = UserService.getCurrentUser();
    if (currentUser == null) return;

    // 清空现有状态
    likeStatus.clear();
    collectStatus.clear();

    // 更新所有推荐香水的点赞和收藏状态
    for (var perfume in recommendPerfumes) {
      likeStatus[perfume.id] = LikeCollectionService.isPerfumeLiked(
        currentUser.id,
        perfume.id,
      );

      collectStatus[perfume.id] = LikeCollectionService.isPerfumeCollected(
        currentUser.id,
        perfume.id,
      );
    }
  }

  // 只更新点赞和收藏状态，不重新加载和清空香水数据
  @action
  void updateLikeCollectStatusOnly() {
    final currentUser = UserService.getCurrentUser();
    if (currentUser == null) return;

    // 更新所有推荐香水的点赞和收藏状态，但不清空现有状态
    for (var perfume in recommendPerfumes) {
      likeStatus[perfume.id] = LikeCollectionService.isPerfumeLiked(
        currentUser.id,
        perfume.id,
      );

      collectStatus[perfume.id] = LikeCollectionService.isPerfumeCollected(
        currentUser.id,
        perfume.id,
      );
    }
  }

  @action
  Future<bool> likePerfume(int perfumeId) async {
    final currentUser = UserService.getCurrentUser();
    if (currentUser == null || UserService.isGuestUser()) {
      return false;
    }

    final result = await LikeCollectionService.likePerfume(
      currentUser.id,
      perfumeId,
    );

    if (result) {
      // 更新点赞状态
      likeStatus[perfumeId] = LikeCollectionService.isPerfumeLiked(
        currentUser.id,
        perfumeId,
      );
    }

    return result;
  }

  @action
  Future<bool> collectPerfume(int perfumeId) async {
    final currentUser = UserService.getCurrentUser();
    if (currentUser == null || UserService.isGuestUser()) {
      return false;
    }

    final result = await LikeCollectionService.collectPerfume(
      currentUser.id,
      perfumeId,
    );

    if (result) {
      // 更新收藏状态
      collectStatus[perfumeId] = LikeCollectionService.isPerfumeCollected(
        currentUser.id,
        perfumeId,
      );
    }

    return result;
  }

  // 获取特定香水的点赞状态
  bool isPerfumeLiked(int perfumeId) {
    final currentUser = UserService.getCurrentUser();
    if (currentUser == null) return false;

    if (likeStatus.containsKey(perfumeId)) {
      return likeStatus[perfumeId] ?? false;
    }

    // 如果缓存中没有，则从服务获取
    final status = LikeCollectionService.isPerfumeLiked(
      currentUser.id,
      perfumeId,
    );
    likeStatus[perfumeId] = status;
    return status;
  }

  // 获取特定香水的收藏状态
  bool isPerfumeCollected(int perfumeId) {
    final currentUser = UserService.getCurrentUser();
    if (currentUser == null) return false;

    if (collectStatus.containsKey(perfumeId)) {
      return collectStatus[perfumeId] ?? false;
    }

    // 如果缓存中没有，则从服务获取
    final status = LikeCollectionService.isPerfumeCollected(
      currentUser.id,
      perfumeId,
    );
    collectStatus[perfumeId] = status;
    return status;
  }

  // 刷新香水数据（包括关联的用户信息）
  @action
  Future<void> refreshPerfumeData() async {
    // 清空现有状态
    likeStatus.clear();
    collectStatus.clear();

    // 重新加载数据
    await loadRecommendData();

    // 强制触发观察者更新
    final temp = recommendPerfumes.toList();
    recommendPerfumes.clear();
    recommendPerfumes.addAll(temp);
  }
}
